var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));
var Tx = require('ethereumjs-tx').Transaction;

//黑窗口第一个地址
var fromAddr = "0x10Db1Bf1759164b22B347092Bf7e343C1aFd6787";
//第一个地址的私钥
var privateKey = Buffer.from('da31646d3239fc8a772585ede15441173b52e0c63ed5f9bc14b02539345e2791', 'hex')
//合约地址
var contractAddr = '0xD022599FCA8Fd260b6CDe04C604024E7eb52f8e2';
//wzabi文件
var abi =[{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_receivers","type":"address[]"},{"name":"_value","type":"uint256"}],"name":"batchTransfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"frozenAccount","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"target","type":"address"},{"name":"freeze","type":"bool"}],"name":"freezeAccount","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"target","type":"address"},{"indexed":false,"name":"frozen","type":"bool"}],"name":"FrozenFunds","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];






//构造函数:
var wzContract = new web3.eth.Contract(abi,contractAddr);

//黑窗口第二个地址
sendSignedTransaction2('0xa8FE05a7574f6E99e9a4f439607Ce12e8391f8FE','5');

function sendSignedTransaction2(toAddr,amount){
   var txData = wzContract.methods.transfer(toAddr,web3.utils.toWei(amount,'gwei')).encodeABI();
   web3.eth.getTransactionCount(fromAddr, 'pending',(err, nonce)=>{
	if(err){
		console.log(err)
	}
	 var rawTx = {
	   nonce: web3.utils.toHex(nonce),
	   gasPrice: web3.utils.toHex(web3.utils.toWei('20','gwei')),
	   gasLimit: web3.utils.toHex(3000000),
	   data: txData,
	   to: contractAddr
	 }
	 var tx = new Tx(rawTx);
	 tx.sign(privateKey);
	 	
	 var serializedTx = tx.serialize();
	
	web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'),(error,hash)=>{
		if(!error){
			console.log(hash)
		}else{
			console.log(error)
		}
	})
})
}
////执行完 拿到交易凭证:  0xfe21661a0cff0bdbaad27d6c3ad323d6780887b8840b2732e3385ec61b47db94


//getTransactionReceipt('0xfe21661a0cff0bdbaad27d6c3ad323d6780887b8840b2732e3385ec61b47db94');

function getTransactionReceipt(Transaction){
	//返回指定交易的收据，使用哈希指定交易。需要指出的是，挂起的交易其收据无效。
	var receipt = web3.eth.getTransactionReceipt(Transaction)
	.then(console.log);
}






